package edu.upol.paradise.hypervisor;

import java.io.File;
import java.util.Random;

import org.apache.log4j.Logger;
import org.kohsuke.args4j.CmdLineException;
import org.kohsuke.args4j.CmdLineParser;
import org.kohsuke.args4j.Option;
import org.kohsuke.args4j.OptionHandlerFilter;

public class HypervisorSettings {
	static final Logger log = Logger.getLogger(HypervisorSettings.class);

	public static HypervisorSettings parse(String[] args) {
		log.info("Loading settings");
		HypervisorSettings hss = new HypervisorSettings();
		CmdLineParser parser = new CmdLineParser(hss);
		try {
			parser.parseArgument(args);
		} catch (CmdLineException e) {
			System.err.println("usage ParadiseHypervisor"
					+ parser.printExample(OptionHandlerFilter.ALL));
			System.exit(0);
		}
		if (hss.getConfig() != null)
			parseXml(hss);
		return hss;
	}

	private static void parseXml(HypervisorSettings hss) {

	}

	private Random random = new Random();

	@Option(name = "--PHVConf", usage = "configuration file location")
	private File config;
	@Option(handler = BooleanValueHandler.class, name = "--PHVInfoService", usage = "run info thread that can be queried by http")
	private boolean infoServer = true;
	@Option(name = "--PHVPort", usage = "Paradise Hypervisor server port")
	private int serverAddress = Math.abs((random.nextInt() + 1024)) % 65535;
	@Option(name = "--PHVServiceTCount", usage = "number of service threads avaialable")
	private int serviceThreads = 2;
	@Option(name = "--PHVInfoPort", usage = "Paradise Hypervisor info thread")
	private int infoServerAddress = 8009;
	@Option(name = "--PHVClustCount", usage = "number of clusters avaialable")
	private int clusterThreads = Runtime.getRuntime().availableProcessors();
	@Option(name = "--PHVReserved", usage = "clusters reserved for master requests")
	private int reservedMasters = 1;

	public File getConfig() {
		return config;
	}

	public void setConfig(File config) {
		this.config = config;
	}

	public boolean isInfoServer() {
		return infoServer;
	}

	public void setInfoServer(boolean infoServer) {
		this.infoServer = infoServer;
	}

	public int getServerAddress() {
		return serverAddress;
	}

	public void setServerAddress(int serverAddress) {
		this.serverAddress = serverAddress;
	}

	public int getInfoServerAddress() {
		return infoServerAddress;
	}

	public void setInfoServerAddress(int infoServerAddress) {
		this.infoServerAddress = infoServerAddress;
	}

	public int getClusterThreads() {
		return clusterThreads;
	}

	public void setClusterThreads(int clusterThreads) {
		this.clusterThreads = clusterThreads;
	}

	public int getReservedMasters() {
		return reservedMasters;
	}

	public void setReservedMasters(int reservedMasters) {
		this.reservedMasters = reservedMasters;
	}
	
	public int getServiceThreads() {
		return serviceThreads;
	}

	public void setServiceThreads(int serviceThreads) {
		this.serviceThreads = serviceThreads;
	}
}
